﻿Imports System.IO
Imports ContentProcessor.ModuleExtension
Imports System.Threading

Public Class FormMain

    Private ImageFiles() As String = {".bmp", ".jpg", ".jpeg", ".png"}
    Private FileList As New Concurrent.ConcurrentBag(Of FileInfo)

    Private Sub FormMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.OuputPath = Me.TSTB_OutputPath.Text
        For i As Double = 1 To 0 Step -0.1
            Me.Opacity = i
            Threading.Thread.Sleep(100)
        Next
    End Sub

    Private Sub FormMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.TSTB_OutputPath.Text = My.Settings.OuputPath
        Me.LB_Files.Size = Me.LB_Files.Parent.Size
        Me.TC_Preview.Size = Me.TC_Preview.Parent.Size
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub ImportContentsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ImportContentsToolStripMenuItem.Click
        Call ImportFiles()
    End Sub

    Private Sub OpenToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripButton.Click
        Call ImportFiles()
    End Sub

    Private Sub ImportDirectoryToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ImportDirectoryToolStripMenuItem.Click
        Dim path = ShowDirectoryBrowseDialog()
        If String.IsNullOrEmpty(path) = False Then
            Call ImportFromDirectory(path)
        End If
    End Sub

    Private Sub BuildToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles BuildToolStripButton.Click
        If String.IsNullOrEmpty(Me.TSTB_OutputPath.Text) = False Then
            Dim fi As New FileInfo(Me.TSTB_OutputPath.Text)
            If Me.LB_Files.Items.Count > 0 Then
                Me.TSSL_Status.Text = "Building..."
                Dim tsk As New Tasks.Task(
                    Sub()
                        Dim lst As New List(Of String)
                        For Each item In FileList
                            If ImageFiles.Contains(item.Extension) And item.Name.ToLower() <> "icon.png" Then
                                Dim targetFileName = item.FullName.Replace(item.Extension, ".xnb")
                                'Runa.Extension.Assets.Texture2D.FromImage(item.FullName, targetFileName)
                                lst.Add(targetFileName)
                            Else
                                lst.Add(item.FullName)
                            End If
                        Next
                        Call MakeZipFile(Me.TSTB_OutputPath.Text, 9, lst.ToArray())
                        Me.TSSL_Status.Text = "Build successfully."
                    End Sub)
                tsk.Start()
            End If
        Else
            MessageBox.Show("Please input the package path.")
        End If
    End Sub

    Private Sub ToolStripButtonBrowseOutputPath_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonBrowseOutputPath.Click
        With SFD
            .Title = "Save package file"
            .Filter = "Zip file(*.zip)|*.zip|All files(*.*)|*.*"
            If .ShowDialog(Me) = DialogResult.OK Then
                Me.TSTB_OutputPath.Text = .FileName
            End If
        End With
    End Sub

    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutBox.ShowDialog(Me)
    End Sub

    Private Sub LB_Files_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles LB_Files.MouseDoubleClick
        Dim listBox = CType(sender, ListBox)
        For i As Integer = 0 To listBox.Items.Count - 1
            Dim r = listBox.GetItemRectangle(i)
            If r.Contains(e.Location) Then
                Dim item As AsPrintable(Of FileInfo) = listBox.Items(i)
                Select Case item.Obejct.Extension.ToLower()
                    Case ".jpg"
                        Call ShowPicture(item.Obejct)
                    Case ".bmp"
                        Call ShowPicture(item.Obejct)
                    Case ".png"
                        Call ShowPicture(item.Obejct)
                    Case ".rb"
                        Call ShowCode(item.Obejct)
                    Case ".xml"
                        Call ShowCode(item.Obejct)
                End Select
            End If
        Next
    End Sub

    Private Sub TC_Preview_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TC_Preview.MouseDoubleClick
        Dim tc = CType(sender, TabControl)
        For index As Integer = 0 To tc.TabPages.Count - 1
            Dim r = tc.GetTabRect(index)
            If r.Contains(e.Location) Then
                tc.TabPages.RemoveAt(index)
            End If
        Next
    End Sub

    Private Sub ShowPicture(ByVal fi As FileInfo)
        If Me.TC_Preview.TabPages.ContainsKey(fi.FullName) Then
            Me.TC_Preview.SelectTab(Me.TC_Preview.TabPages(fi.FullName))
        Else
            Dim tb As New ImageTab(fi.FullName)
            Me.TC_Preview.TabPages.Add(tb)
            Me.TC_Preview.SelectTab(tb)
        End If
    End Sub

    Private Sub ShowCode(ByVal fi As FileInfo)
        If Me.TC_Preview.TabPages.ContainsKey(fi.FullName) Then
            Me.TC_Preview.SelectTab(Me.TC_Preview.TabPages(fi.FullName))
        Else
            Dim tb As New TextTab(fi.FullName)
            Me.TC_Preview.TabPages.Add(tb)
            Me.TC_Preview.SelectTab(tb)
        End If
    End Sub

    Private Function ShowDirectoryBrowseDialog() As String
        With FBD
            .RootFolder = System.Environment.SpecialFolder.Desktop
            .Description = "Please choose a directory to be scanned."
            .ShowNewFolderButton = False
            If .ShowDialog(Me) = DialogResult.OK Then
                Return .SelectedPath
            Else
                Return ""
            End If
        End With
    End Function

    Private Sub ImportFiles()
        With OFD
            .Title = "Import files"
            .Filter = "All files(*.*)|*.*"
            .Multiselect = True
            If .ShowDialog(Me) = DialogResult.OK Then
                Me.LB_Files.Items.Clear()
                Me.TC_Preview.TabPages.Clear()
                For Each fn In .FileNames
                    Dim fi As New IO.FileInfo(fn)
                    FileList.Add(fi)
                    Me.LB_Files.AddItem(New AsPrintable(Of FileInfo)(fi, Function(obj) obj.Name),
                        Function(item As AsPrintable(Of FileInfo))
                            If item.Obejct.Name.ToLower().Contains("font") Then Return True
                            Return item.Obejct.Extension.Contains(".xnb") = False
                        End Function)
                Next
            End If
        End With
    End Sub

    Private Sub ImportFromDirectory(ByVal path As String)
        If IO.Directory.Exists(path) Then
            Call CheckFilesInDirectory(path, "",
                Sub(fi)
                    FileList.Add(fi)
                    Me.LB_Files.AddItem(New AsPrintable(Of FileInfo)(fi, Function(obj) obj.Name),
                        Function(item As AsPrintable(Of FileInfo))
                            If item.Obejct.Name.ToLower().Contains("font") Then Return True
                            Return item.Obejct.Extension.Contains(".xnb") = False
                        End Function)
                End Sub, Sub(ar) CType(ar.AsyncState, Action(Of FileInfo)).EndInvoke(ar))
        End If
    End Sub

End Class